package org.bigdata.service;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URI;

@Service
public class HadoopService {

    private static final String HDFS_URL = "hdfs://namenode:8020";

    public void uploadFile(MultipartFile file, String hdfsPath) throws IOException {
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(HDFS_URL), configuration);

        // 上传文件到HDFS
        Path path = new Path(hdfsPath);
        fs.copyFromLocalFile(false, true, new Path(String.valueOf(file.getInputStream())), path);
        fs.close();
    }
}

